package cn.sytton.taffe.web.sys.service.impl;

import cn.sytton.taffe.excel.ExcelImport;
import cn.sytton.taffe.excel.model.ExcelImportResult;
import cn.sytton.taffe.framework.base.entity.PageParam;
import cn.sytton.taffe.framework.base.service.BaseService;
import cn.sytton.taffe.web.sys.entity.RoleEntity;
import cn.sytton.taffe.web.sys.entity.RoleUserEntity;
import cn.sytton.taffe.web.sys.entity.UserEntity;
import cn.sytton.taffe.web.sys.mapper.RoleMapper;
import cn.sytton.taffe.web.sys.mapper.RoleUserMapper;
import cn.sytton.taffe.web.sys.service.IRoleService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

/**
 * 角色服务层
 *
 * @author skyrock
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class RoleService extends BaseService<RoleMapper, RoleEntity> implements IRoleService {

    private final RoleUserMapper roleUserMapper;

    @Override
    public PageInfo<UserEntity> findUserListPage(String roleId, PageParam pp) {
        setPageParams(pp);
        boolean exclude = pp.getParams().containsKey("exclude") && Boolean.parseBoolean(pp.getParams().get("exclude").toString());
        return new PageInfo<>(exclude ?  roleUserMapper.findUserListForExclude(roleId) : roleUserMapper.findUserList(roleId));
    }

    @Override
    public List<RoleEntity> findByIds(String[] ids) {
        return super.listByIds(Arrays.asList(ids));
    }

    @Override
    public void addUsers(RoleUserEntity entity) {
        for(String userId : entity.getUserIds()) {
            entity.setUserId(userId);
            roleUserMapper.create(entity);
        }
    }

    @Override
    public void removeUsers(RoleUserEntity entity) {
        if (entity.getUserIds() != null && entity.getUserIds().size() > 0) {
            roleUserMapper.delete(Wrappers.<RoleUserEntity>query().lambda()
                    .eq(RoleUserEntity::getRoleId, entity.getRoleId())
                    .in(RoleUserEntity::getUserId, entity.getUserIds()));
        }
    }

    @Override
    public void updateRoleStatus(RoleEntity entity) {
        updateWithUpdateField(new LambdaUpdateWrapper<RoleEntity>()
                .set(RoleEntity::getRoleStatus, entity.getRoleStatus())
                .eq(RoleEntity::getRoleId, entity.getRoleId()));
    }

    @Override
    public String importData(MultipartFile file) {
        ExcelImport<RoleEntity> excelImport = new ExcelImport<>(RoleEntity.class, file);
        excelImport.importExcel();
        for (ExcelImportResult<RoleEntity> data : excelImport.getResult()) {
            try {
                if (data.isSuccess()) {
                    create(data.getEntity());
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                data.fail(e.getMessage());
            }
        }
        return excelImport.writeImportResult();
    }
}
